--[[
    desc: 新版风暴峡湾buff选择
    author:Hoo
    time:2022-03-07 18:02:28
]]

---@class UIChaosTestBuffChoice
local UIChaosTestBuffChoice = UIBase.New(PanelResNames.UIChaosTestBuffChoice)
local M = UIChaosTestBuffChoice
local K_BUFF = 'buff_'

function M.Init(func)
    UIBase.Init(M, func)
end

--- buff数据结构
---@alias BuffStruct {id: number, type: number, score: number, name: string, desc: string, item: GObject, sort: number}

---@protected
function M:OnCreate()
    self:RegisterOnClick(self:GetChild("BgClose"), self.onClickClose, self)
    self:RegisterOnClick(self:GetChild("btnBack"), self.onClickClose, self)

    -- 当前倍率
    self.txtPoint = self:GetChild("Text_Point")

    -- 展示buff列表
    self.ctrlState = self:GetController("state")
    self.listBuffShow = self:GetChild("showBuff")
    self:SetDynamicList(self.listBuffShow, self.showBuffItemRenderer, self)
    
    -- 噩梦buff
    self.listBuff1 = self:GetChild("buffList1")
    self:SetDynamicList(self.listBuff1, self.buffItemRenderer1, self, true)
    -- 困难buff
    self.listBuff2 = self:GetChild("buffList2")
    self:SetDynamicList(self.listBuff2, self.buffItemRenderer2, self, true)
    -- 简单buff
    self.listBuff3 = self:GetChild("buffList3")
    self:SetDynamicList(self.listBuff3, self.buffItemRenderer3, self, true)

    self.listBuff1.onClickItem:Add(self.onClickItemBuff1, self)
    self.listBuff2.onClickItem:Add(self.onClickItemBuff2, self)
    self.listBuff3.onClickItem:Add(self.onClickItemBuff3, self)
end
---@protected
function M:OnEnable()
    --- 当前所选buff
    ---@type BuffStruct[]
    self.buffArr = {}

    ---@type table<string, BuffStruct>
    self.buffDict = {}

    ---@type BuffStruct[][]
    self.buffDatas = {}

    -- 解析buff配置
    local buffDatas = logicMgr.TrialsManager.getBuffDatas()
    local currBuffs = logicMgr.TrialsManager.loadTrialsBuff()
    for i = 1, #buffDatas do
        local data = buffDatas[i]
        self.buffDatas[i] = {}

        for j = 1, #data.options do
            local id, score = data.options[j], data.scores[j]
            local cfg  = dataTable.getDataTableByStringId('t_skill', tostring(id))
            local buff = {
                id = id, score = score, name = cfg.f_SkillName, desc = cfg.f_Desc, sort = i * 10 + j
            }
            table.insert(self.buffDatas[i], buff)
            if table.contains(currBuffs, id) then
                self.buffDict[K_BUFF .. id] = buff
            end
        end
    end

    self:refreshBuffList()
    self.listBuff1.numItems = table.getn(self.buffDatas[1])
    self.listBuff2.numItems = table.getn(self.buffDatas[2])
    self.listBuff3.numItems = table.getn(self.buffDatas[3])
end
---@protected
function M:OnDisable()
    self.buffArr   = nil
    self.buffDatas = nil
    self.buffDict  = nil
end
---@protected
function M:OnDestroy()
    self.listBuff1.onClickItem:Clear()
    self.listBuff2.onClickItem:Clear()
    self.listBuff3.onClickItem:Clear()
end

--- 刷新buff列表
---@private
function M:refreshBuffList()
    self.buffArr = {}
    local point3 = 0
    for _, v in pairs(self.buffDict) do
        point3 = point3 + v.score
        table.insert(self.buffArr, v)
    end
    table.sort(self.buffArr, function (a, b) return a.sort < b.sort end)

    local point1 = logicMgr.TrialsManager.getLevelPoint()
    local point2 = logicMgr.TrialsManager.getDurationPoint()
    local points = point1 * point2 + point3
    self.txtPoint.text = string.format(getLanguage("UIChaosTestMagnification"), points .. '')
    self.listBuffShow.numItems   = #self.buffArr
    self.ctrlState.selectedIndex = #self.buffArr == 0 and 1 or 0
    logicMgr.TrialsManager.setBuffPoint(point3)
end

--- 展示buff列表
---@private
---@param index number  索引
---@param obj   GObject buff item对象
function M:showBuffItemRenderer(index, obj)
    local data     = self.buffArr[index]
    local txtPoint = obj:GetChild("buffPoint")
    local txtName  = obj:GetChild("buffName")
    local txtDesc  = obj:GetChild("buffDesc")
    txtPoint.text = data.score .. ''
    txtName.text  = getLanguage(data.name)
    txtDesc.text  = getLanguage(data.desc)
end

--- 噩梦buff列表
---@private
---@param index number  索引
---@param obj   GObject buff item对象
function M:buffItemRenderer1(index, obj)
    local data = self.buffDatas[1][index]
    self:setBuffData(obj, data)
end
--- 困难buff列表
---@private
---@param index number  索引
---@param obj   GObject buff item对象
function M:buffItemRenderer2(index, obj)
    local data = self.buffDatas[2][index]
    self:setBuffData(obj, data)
end
--- 简单buff列表
---@private
---@param index number  索引
---@param obj   GObject buff item对象
function M:buffItemRenderer3(index, obj)
    local data = self.buffDatas[3][index]
    self:setBuffData(obj, data)
end

--- buff ui
---@private
---@param item GObject    buff item对象
---@param data BuffStruct buff数据
function M:setBuffData(item, data)
    if not item or not data then
        return
    end

    local icon = item:GetChild("n3")
    icon.url  = EnumConst.AtlasPath.SkillAtlas .. data.id
    data.item = item

    -- 已选择
    local selectBuffData = self.buffDict[K_BUFF .. data.id]
    if selectBuffData ~= nil then
        self:setBuffItemSelected(item, true)
    end
end

--- 设置buff item状态
---@param item       GObject item对象
---@param isSelected boolean 是否选中
---@private
function M:setBuffItemSelected(item, isSelected)
    local ctrl = item:GetController("select")
    if ctrl then
        ctrl.selectedIndex = isSelected and 1 or 0
    end
end

--- 选择buff：噩梦
---@private
function M:onClickItemBuff1(item)
    local obj   = item.data
    local index = self.listBuff1:GetChildIndex(obj)
    self:onChooseBuff(1, index + 1, obj)
end
--- 选择buff：困难
---@private
function M:onClickItemBuff2(item)
    local obj   = item.data
    local index = self.listBuff2:GetChildIndex(obj)
    self:onChooseBuff(2, index + 1, obj)
end
--- 选择buff：简单
---@private
function M:onClickItemBuff3(item)
    local obj   = item.data
    local index = self.listBuff3:GetChildIndex(obj)
    self:onChooseBuff(3, index + 1, obj)
end
---@private
function M:onChooseBuff(tag, index, item)
    local data = self.buffDatas[tag][index]

    local k = K_BUFF .. data.id
    if self.buffDict[k] then
        self.buffDict[k] = nil
        self:setBuffItemSelected(item, false)
    else
        self.buffDict[k] = data
        self:setBuffItemSelected(item, true)
    end
    
    self:refreshBuffList()
end

--- 存储buff
---@private
function M:saveBuffData()
    if self.buffArr == nil then return end

    local list = {}
    for i = 1, #self.buffArr do
        table.insert(list, self.buffArr[i].id)
    end
    logicMgr.TrialsManager.saveTrialsBuff(list)
end

--- 关闭界面
---@private
function M:onClickClose()
    self:saveBuffData()
    self:OnBack()
    -- closeUI(PanelResNames.UIChaosTestBuffChoice)
end

return M
